﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using Treblotto_TabletDefs;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows.Controls;
using System.Windows.Input;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;

namespace TTIServer
{

	[Serializable]
	public class TabletType
	{



	}

	[Serializable]
	public class TouchDeivceID
	{




	}



	[Serializable]
	public class TabletSettings
	{
		public int TabletID;
		public string Name;
		public ushort PID;
		public string ModelNumber;
		public String SerialNumber;
		public int TouchDeviceID;
		
		////These are all of the Prox'd in Pens
		List<EMRDevice> PensWithSettings;

		//This is the Active Pen
		PenSettings StylusPenSettings;

		public void setStylusPenSettings(PenSettings inPenSettings)
		{

			StylusPenSettings = inPenSettings;

		}
		public List<EMRDevice> GetPensProxedIn()
		{

			return PensWithSettings;

		}

		public PenSettings GetCurrentPenSettings()
		{
			return StylusPenSettings;
		}

		public void SetCurrentPenSettings( List<EMRDevice> inPensProxedIn)
		{
			PensWithSettings = inPensProxedIn;

		}

		//This is only valid while the program is running
		[NonSerialized]
		WCintiqHID.Cintiq_K CintiqReference;
		
		public void SetCintiqReference( WCintiqHID.Cintiq_K inReference)
		{

			CintiqReference = inReference;

		}


		/// <summary>
		/// This is the Button settings for a specific Cintiq.
		/// </summary>
		ExpressKeySettings CintiqButtonSettings;

		public void SetCintiqButtonSettings ( ExpressKeySettings inCintiqButtonSettings)
		{

			CintiqButtonSettings = inCintiqButtonSettings;

		}

		public ExpressKeySettings GetCintiqButtonSettings ()
		{
			return CintiqButtonSettings;
		}




		// Touch Pad or Ring

		//Express Keys

		//Switch Them here for various applications?


	}

	public static class DefaultTabletSettings
	{

		public static TabletSettings GetDefaultTabletSettings()
		{
			TabletSettings defTabletSettings = new TabletSettings();

			//No default for applications!

			return defTabletSettings;

		}

	}

	public static class CintiqSettinsCollectionLoader
	{

		public static CintiqSettingsCollection LoadTTISettings()
		{
			CintiqSettingsCollection savedCollection;

			IsolatedStorageFile TTIStorage = IsolatedStorageFile.GetUserStoreForAssembly();
			IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
			IsolatedStorageFileStream TTIFS = null;

			try
			{
				TTIFS = new IsolatedStorageFileStream("CintiqSettingsCollection", System.IO.FileMode.Open, TTIStorage);
				savedCollection = (CintiqSettingsCollection)formatter.Deserialize(TTIFS);
			}
			catch (EndOfStreamException e)
			{
				
				savedCollection = new CintiqSettingsCollection();
				savedCollection.AddTabletToCollection(DefaultTabletSettings.GetDefaultTabletSettings());

			}
			catch (FileNotFoundException e)
			{

				savedCollection = new CintiqSettingsCollection();
				savedCollection.AddTabletToCollection(DefaultTabletSettings.GetDefaultTabletSettings());

			}
			catch ( SerializationException e )
			{

				savedCollection = new CintiqSettingsCollection();
				savedCollection.AddTabletToCollection(DefaultTabletSettings.GetDefaultTabletSettings());

			}
			finally
			{
				if (TTIFS != null)
				TTIFS.Close();

			}

			return savedCollection;

		}

		public static bool SaveTTISettings(CintiqSettingsCollection settingsToSave)
		{

			IsolatedStorageFile TTIStorage = IsolatedStorageFile.GetUserStoreForAssembly();
			IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

			IsolatedStorageFileStream TTIFS = new IsolatedStorageFileStream("CintiqSettingsCollection", System.IO.FileMode.Create, TTIStorage);

			try
			{
				formatter.Serialize(TTIFS, settingsToSave);
			}
			catch (SerializationException e)
			{

				Console.WriteLine("Error with serialization: {0}", e.ToString());
				TTIFS.Close();
				return false;
			}


			TTIFS.Close();
			return true;
		}

	}

	[Serializable]
	public class CintiqSettingsCollection
	{

		List<TabletSettings> AllTabletSettings;
		List<CustomApplicationActions> CustomizedApplicationSettings;

		//Add more filtering options as I learn how to get various numbers
		public TabletSettings GetTabletSettingsForPID( ushort PID )
		{

			foreach (TabletSettings TS in AllTabletSettings)
			{

				if (TS.PID == PID)
					return TS;
			}

			return null;

		}

		public List<TabletSettings> getAllTabletSettings() 
		{ 
			return AllTabletSettings; 
		}

		public List<CustomApplicationActions> getCustomizedApplicationSettings() 
		{ 
			return CustomizedApplicationSettings; 
		}

		public void AddTabletToCollection (TabletSettings newTabletSettings )
		{

			AllTabletSettings.Add(newTabletSettings);

		}



		public CintiqSettingsCollection()
		{
			AllTabletSettings = new List<TabletSettings>(0);
			CustomizedApplicationSettings = new List<CustomApplicationActions>(0);
		}

	}

}
